package com.kermel.andromeda.engine;

import java.util.Timer;
import java.util.TimerTask;

public class CurrentTimeTicker {
    public static final String TIMER_NAME = "CurrentTimeTickerTimer";
    
    private CurrentTimeSubscriber subscriber;
    private Timer timer;
    private TimerTask timerTask;
    
    public CurrentTimeTicker(CurrentTimeSubscriber subscriber) {
        this.subscriber = subscriber;
        timerTask = new PeriodicCallback();
        timer = new Timer(TIMER_NAME, true);
        timer.schedule(timerTask, 0, 1000);
    }
    
    public CurrentTimeSubscriber getSubscriber() {
        return subscriber;
    }
    
    private class PeriodicCallback extends TimerTask {
        @Override
        public void run() {
            long currentTime = System.currentTimeMillis();
            subscriber.onCurrentTimeTick(currentTime);
        }
    }
}
